using UnityEngine;
using System.Collections.Generic;
using Minix;

//初速度,重力加速度,分段Raycast,时间迭代位置
public class Blink : MonoBehaviour {
    public Vector3 fG = new Vector3(0, -9.8f, 0);
    public float iV = 10f;
    public float projectile_time = 3f;
    public float projectile_step = 0.1f;
    public LayerMask layerMask = 0xffff;
    public Transform ori;
    public LineRenderer lineRenderer;
    public GameObject landMark;

    public GameObject move_target;

    public bool debug_draw = false;
    public Color[] debug_draw_colors = new Color[] { Color.red, Color.green, Color.blue };

    bool raycast = false;
    bool hit = false;
    Vector3 hit_point = Vector3.zero;

    [ContextMenu("Move")]
    public void Move() {
        if(raycast&&hit){
            move_target.transform.position = hit_point;
            raycast = false;
        }
    }
    [ContextMenu("Raycast")]
    public void Raycast(){
        raycast = true;
    }
    [ContextMenu("Cancel")]
    public void Cancel(){
        raycast = false;
    }

    public void ProjectileRaycast(){
        Vector3 point = new Vector3();
        List<Vector3> pts = new List<Vector3>();
        hit = Projectile.Raycast(ori.position,ori.forward*iV,fG,out point,out pts,
        projectile_step,projectile_time,layerMask);
        landMark.SetActive(hit);
        if(hit) {
            landMark.transform.position = point;
            hit_point = point;
        }
        //Debug Draw
        if(debug_draw) {
            for(int i = 0; i < pts.Count - 1; i++) {
                Debug.DrawLine(pts[i], pts[i + 1],debug_draw_colors[i%debug_draw_colors.Length]);
            }
        }
        //Draw
        lineRenderer.positionCount = pts.Count;
        if(lineRenderer != null) {
            lineRenderer.SetPositions(pts.ToArray());
        }
    }

    private void Update() {
        if(raycast){
            ProjectileRaycast();
        }
        landMark.SetActive(raycast);
        if(lineRenderer != null){
            lineRenderer.enabled = raycast;
        }
    }
}